package sk.tuke.homescreen;

import sk.tuke.android4all.controls.CustomFragment;
import sk.tuke.android4all.listeners.SwipeListener;
import sk.tuke.android4all.services.Tts;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ContextmenuFragment extends CustomFragment {
		
	private static String TAG = "ContextmenuFragment";
	private Fragment mInstance = null;
	private String mAppName = null;
	private String mAppFullPackageName = null;
	private Activity mActivity = null;
	private int mTag = 0;
	private int touchCounter = 0;
    private final int TOUCH_INTENSITY = 4;
    private Tts mSpeechEngine = null;
    private GeneralContext mGeneralContext = null;
    
		public ContextmenuFragment(GeneralContext aContextObj_in, String aAppName_in, String aAppFullPackageName_in){
			mAppName = aAppName_in;
			mAppFullPackageName = aAppFullPackageName_in;
			mInstance = this;
			mActivity = aContextObj_in.getActivity();
			mSpeechEngine = aContextObj_in.getSpeechEngine();
			mGeneralContext = aContextObj_in;
		}
		
		@Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	            Bundle savedInstanceState) {
	        View rootView = inflater.inflate(R.layout.fragment_contextmenu, container, false);	        
	        return rootView;
	    }
		
		@Override
	    public void onActivityCreated(Bundle savedInstanceState) {
	        super.onActivityCreated(savedInstanceState);
	        ((TextView) getView().findViewById(R.id.contextmenu_title)).setText(mAppName);
	        setItemControl(getView(), R.id.contextmenu_quickpick, R.string.menu_quickpick);
	        setItemControl(getView(), R.id.contextmenu_delete, R.string.menu_delete);
	        
	        getView().setOnTouchListener(new SwipeListener(mActivity){

				@Override
				public void onDoubleTapAction(int singleTouchCountBefore) {
					Log.i(TAG, "onDoubleTapAction mTag is: " + mTag);
					if (mTag == R.id.contextmenu_quickpick) {
						quickpickSelected();
					} else if (mTag == R.id.contextmenu_delete) {
						deleteSelected();
					}
				}

				@Override
				public void onZoomInAction() {
					Log.i(TAG, "OnZoomInAction");
					mSpeechEngine.sayMessage(getResources().getString(R.string.voice_contextmenu_closed));
					getFragmentManager().beginTransaction().replace(R.id.container, new MenuFragment()).commit();
				}

				@Override
				public void onZoomOutAction() {
					Log.i(TAG, "OnZoomOutAction");
					mSpeechEngine.sayMessage(getResources().getString(R.string.voice_quickpick_opened));
					getFragmentManager().beginTransaction().replace(R.id.container, new QuickPickFragment(mGeneralContext)).commit();					
				}} );
	    }
		
		private void setItemControl(View aRootView_in, final int aControlId,final int aTextId){
			aRootView_in.findViewById(aControlId).setOnGenericMotionListener(new View.OnGenericMotionListener() {
	            @Override
	            public boolean onGenericMotion(View view, MotionEvent event) {
	                if (mTag != aControlId && touchCounter>TOUCH_INTENSITY) {
	                    touchCounter = 0;
	                    mSpeechEngine.sayMessage(mInstance.getResources().getString(aTextId));
	                    mTag = aControlId;
	                }else{
	                    touchCounter++;
	                }
	                return false;
	            }
	        });
		}
		
		private void deleteSelected() {
			try{
				Uri packageURI = Uri.parse(mAppFullPackageName);
				Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
				startActivity(uninstallIntent);			
			}catch(Exception ex){
				mSpeechEngine.sayMessage(getResources().getString(R.string.delete_failed));
			}
		}

		private void quickpickSelected() {
			getFragmentManager().beginTransaction().replace(R.id.container, new QuickPickAssignmentFragment(mAppName,mAppFullPackageName,mGeneralContext)).commit();	
		}

		@Override
		public void returnAction() {			
			Log.i(TAG, "returnAction");
			if(getFragmentManager() != null){		
				getFragmentManager().beginTransaction().replace(R.id.container, new MenuFragment()).commit();	
			}
		}

}
